﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001线性列表
{
    class Sequence<T> : IListDS<T>
    {
        //public T this[int index] => throw new NotImplementedException();
        
        private T[] data;
        private int count;
        public Sequence(int size)
        {
            data = new T[size];
            count = 0;
        }
        public Sequence() : this(10)
        {
        }

        public T this[int index] {
            get { return GetEle(index); }
        }


        public void Add(T item)
        {
            if (count == data.Length)
            {
                Console.WriteLine("存满！");
            }
            else
            {
                data[count] = item;
                count++;
            }
        }

        public void Clear()
        {
            count = 0;
        }

        public T Delete(int index)
        {
            T item = data[index];
            for (int i = index + 1; i < count; i++)
            {
                data[i - 1] = data[i];
            }
            count--;
            return item;
        }

        public T GetEle(int index)
        {
            if (index >= 0 && index <= count - 1)
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("不存在");
                return default(T);//default方法取得默认值，data[0]
            }
        }
        /// <summary>
        /// 取得数据的个数
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            return count;
        }

        public void Insert(T item, int index)
        {
            for (int i = count; i >= index; i--)
            {
                data[i + 1] = data[i];
            }
            count++;
            data[index] = item;
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public int Locate(T value)
        {
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].Equals(value))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
